package com.lx.bizimpl;

import java.util.List;

import javax.xml.crypto.Data;

import org.apache.commons.lang.time.DateUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date; 
import java.util.Calendar; 
import java.text.SimpleDateFormat; 

import com.lx.biz.IEmploee;
import com.lx.domain.Dept;
import com.lx.domain.Emploee;

@Transactional
@Service("iemploeeimpl")
public class IEmploeeimpl implements IEmploee {
	@Autowired
	private SessionFactory sessionFactory;  

	public void setSessionFactory(SessionFactory sessionFactory) {  
	    this.sessionFactory = sessionFactory;  
	}  
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Emploee> geteEmploees(String name, String dept_id) {
		
		  String hql = null;
		  
		  if (name==null || name.equals("")) {
			  if (dept_id==null || dept_id.equals("")) {
				  hql = "select e from Emploee e LEFT JOIN e.dept ORDER BY e.dept.ord asc ,e.ord asc" ;
				 /* hql = " from Emploee e  " ;*/
				  Session session = sessionFactory.getCurrentSession();
				  Query query = session.createQuery(hql);
				  List<Emploee> list =query.list();
				  return list; 
			}
			else  {
				hql = "select e from Emploee e Where dept_id = ? ORDER BY e.ord asc";
				Integer id=Integer.parseInt(dept_id);
				Query query = sessionFactory.getCurrentSession().createQuery(hql);
				query.setInteger(0, id);
				  return query.list(); 
			}
			  
		}
		  else  {
			if (dept_id==null || dept_id.equals("")) {
				hql = "select e from Emploee e LEFT JOIN e.dept Where name like ? ORDER BY e.dept.ord asc ,e.ord asc"; 
				Query query = sessionFactory.getCurrentSession().createQuery(hql);
				query.setString(0, "%"+name+"%");   //模糊查询
				
				return query.list(); 
			}
			else {
				 Integer id=Integer.parseInt(dept_id);
				 hql = "select e from Emploee e LEFT JOIN e.dept Where name like ? and dept_id = ? ORDER BY e.dept.ord asc ,e.ord asc"; 
				 Query query = sessionFactory.getCurrentSession().createQuery(hql);
				  	query.setString(0, "%"+name+"%");    //模糊查询
			        query.setInteger(1, id);
			        return query.list();
			}
		}
	}
	
	@Override
	public void update(Emploee emploee) {
		Session session= sessionFactory.getCurrentSession();
		if (emploee.getOrd()== 0) {
			String hql1="select max(e.ord) from Emploee e" ;
			int max_ord = (int)sessionFactory.getCurrentSession().createQuery(hql1).uniqueResult();
		
			emploee.setOrd(max_ord+1);
			Date now = new Date() ;
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");//可以方便地修改日期格式
			try {
				String datestrString =dateFormat.format(now) ;
				Date date =dateFormat.parse(datestrString);
				emploee.setCreatTime(date);
			} catch (Exception e) {
				
			}
		}
		
		session.saveOrUpdate(emploee);

	}

	@Override
	public void delete(String ids) {
		//批量删除方法
		Session session= sessionFactory.getCurrentSession();
		String hql = "DELETE from Emploee e WHERE ";
		 String[] str =ids.split(",");
		 for (int i = 0; i < str.length; i++) {
			 if (i==0) {
				 //前端传过来的为string类型 先转型 此处用hql连接方式组合
				hql= hql +"id =" +Integer.parseInt(str[i]);
			}else {
				hql =hql +"or id =" +Integer.parseInt(str[i]);
			}
		}
		session.createQuery(hql).executeUpdate();

	}

	@Override
	public Emploee findEmploee(String id) {
		Emploee emploee = null;
		if (id != null || !id.equals("")) {
			String hql = "from Emploee Where id = ?";
			Integer id1=Integer.parseInt(id);
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
			query.setInteger(0, id1);
			emploee =(Emploee)query.uniqueResult();
			
		}
		
		return emploee;
		
		
	}

	@Override
	public void insert(Emploee emploee) {
		// TODO Auto-generated method stub

	}

	@Override
	public void edit_ord(Emploee emploee1,Emploee emploee2) {
		Session session= sessionFactory.getCurrentSession();
		int ord1 = emploee1.getOrd();
		int ord2 = emploee2.getOrd();
        emploee1.setOrd(ord2);
        emploee2.setOrd(ord1);
        session.saveOrUpdate(emploee1);
        session.saveOrUpdate(emploee2);
	}
}
